﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZhwangTool
{
    /// <summary>
    /// PictureViewControl.xaml 的交互逻辑
    /// </summary>
    public partial class PictureViewControl : UserControl
    {
        public PictureViewControl()
        {
            InitializeComponent();
        }

        ObservableCollection<LVData> lVDatas = new ObservableCollection<LVData>();
        
        private String uri;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lVDatas.Clear();

            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog.SelectedPath;
            }

            double objWidth = 200;




            try
            {
                ImageTexture2DView(folderBrowserDialog.SelectedPath,objWidth);
            }
            catch (Exception)
            {
                myImage.Source = null;
            }
            
        }

        private void ImageTexture2DView(string path)
        {
            var apps = System.IO.Directory.GetFiles(path);

            foreach (var app in apps)
            {
                var fi = new System.IO.FileInfo(app);

                if (fi.Extension.ToLower() == ".jpg")
                {
                    string tmpName = fi.Name.Remove(fi.Name.LastIndexOf("."));

                    // Create source
                    BitmapImage bitmapImage = new BitmapImage();

                    // BitmapImage.UriSource must be in a BeginInit/EndInit block

                    bitmapImage.BeginInit();

                    bitmapImage.UriSource = new Uri(fi.FullName);


                    // To save significant application memory, set the DecodePixelWidth or
                    // DecodePixelHeight of the BitmapImage value of the image source to the desired
                    // height or width of the rendered image. If you don't do this, the application will
                    // cache the image as though it were rendered as its normal size rather than just
                    // the size that is displayed.
                    // Note: In order to preserve aspect ratio, set DecodePixelWidth
                    // or DecodePixelHeight but not both.

                    bitmapImage.DecodePixelWidth = 200;

                    bitmapImage.EndInit();
                    //set image source
                  
                    LVData lVData = new LVData { Name = tmpName, Pic = bitmapImage ,FullName=fi.FullName};

                    lVDatas.Add(lVData);
                    
                }
                
            }
            listFileManager.ItemsSource = lVDatas;
        }


        private void ImageTexture2DView(string path,double objWidth)
        {
            var apps = System.IO.Directory.GetFiles(path);

            foreach (var app in apps)
            {
                var fi = new System.IO.FileInfo(app);

                if (fi.Extension.ToLower() == ".jpg")
                {
                    string tmpName = fi.Name.Remove(fi.Name.LastIndexOf("."));

                    // Create source
                    BitmapImage bitmapImage = new BitmapImage();

                    // BitmapImage.UriSource must be in a BeginInit/EndInit block

                    bitmapImage.BeginInit();

                    bitmapImage.UriSource = new Uri(fi.FullName);


                    // To save significant application memory, set the DecodePixelWidth or
                    // DecodePixelHeight of the BitmapImage value of the image source to the desired
                    // height or width of the rendered image. If you don't do this, the application will
                    // cache the image as though it were rendered as its normal size rather than just
                    // the size that is displayed.
                    // Note: In order to preserve aspect ratio, set DecodePixelWidth
                    // or DecodePixelHeight but not both.

                    bitmapImage.DecodePixelWidth = (int)objWidth;

                    bitmapImage.EndInit();
                    //set image source

                    LVData lVData = new LVData { Name = tmpName, Pic = bitmapImage, FullName = fi.FullName };

                    lVDatas.Add(lVData);

                }

            }
            listFileManager.ItemsSource = lVDatas;
        }






        private String GetFilepath()
        {
            String Filepath;

            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();

            ofd.Filter = "JPEG Files(*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp";

            ofd.InitialDirectory = "d:\\";

            if (ofd.ShowDialog().Value==true)
            {
                Filepath = ofd.FileName;
                return Filepath;
            }
            return null;
        }

        private void ListFileManager_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            
        }

        private void ListFileManager_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            
            var selectedItem = listBox.SelectedItem as LVData;

            if (selectedItem!=null)
            {
                //MessageBox.Show(selectedItem.FullName);

                
                BitmapImage myBitmapImage = new BitmapImage(new Uri(selectedItem.FullName));

                myImage.Source = myBitmapImage;

                myBitmapImage = null;

            }
            
        }

        private void MyImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Image image = sender as Image;

            if (image == null)
            {
                return;
            }

            TransformGroup group = myImage.FindResource("ImageCompareResources") as TransformGroup;

            var tgnew = group.CloneCurrentValue();

            double angle = 0;

            if (tgnew != null)
            {
                RotateTransform rt = tgnew.Children[2] as RotateTransform;

                angle = rt.Angle % 360;

            }

            Point point = e.GetPosition(image);

            //MessageBox.Show(point.X.ToString("0.00")+"---"+point.Y.ToString("0.00"));

            point = new Point(point.X - image.ActualWidth / 2, point.Y - image.ActualHeight / 2);

            double scale = e.Delta * 0.001;

            ZoomImage(group, point, scale);
        }

        private static void ZoomImage(TransformGroup group, Point point, double scale)
        {

            Point pointToContent = group.Inverse.Transform(point);

            ScaleTransform transform = group.Children[0] as ScaleTransform;

            if (transform.ScaleX + scale < 0.5)
            {
                return;
            }

            transform.ScaleX += scale;
            transform.ScaleY += scale;

            TranslateTransform transform1 = group.Children[1] as TranslateTransform;

            transform1.X = -1 * ((pointToContent.X * transform.ScaleX) - point.X);

            transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y);

        }

    }


    public class LVData
    {
        string name;
        string fullName;
        BitmapImage pic;

        public string Name { get => name; set => name = value; }
        public BitmapImage Pic { get => pic; set => pic = value; }
        public string FullName { get => fullName; set => fullName = value; }
    }

}
